package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.LandMarkListAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.SearchLandMarkBean;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Weight.FlowLayout;
import com.weiyi.mashangzhu.Weight.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LandMarkActivity extends BaseActivity {

    @BindView(R.id.edtext)
    EditText edtext;
    @BindView(R.id.lv_landmark)
    MyListView lvLandmark;
    @BindView(R.id.lv_host)
    MyListView lvHost;
    @BindView(R.id.lv_house)
    MyListView lvHouse;
    @BindView(R.id.layout_searchresult)
    RelativeLayout layoutSearchresult;
    @BindView(R.id.layout_landmark)
    LinearLayout layoutLandmark;
    @BindView(R.id.layout_host)
    LinearLayout layoutHost;
    @BindView(R.id.layout_house)
    LinearLayout layoutHouse;
    private String[] mVals2;
    private FlowLayout mFlowLayout;
    String city = "";
    String select = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land_mark);
        ButterKnife.bind(this);
        mFlowLayout = (FlowLayout) findViewById(R.id.id_flowlayout);
        city = getIntent().getStringExtra("city");
        initData();
        initEditText();
    }

    private void initEditText() {
        edtext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
                filterData(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void filterData(String string) {
        if (TextUtils.isEmpty(string)) {
            layoutSearchresult.setVisibility(View.GONE);
        } else {
            layoutSearchresult.setVisibility(View.VISIBLE);
            OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("cityname", city)
                    .addParams("keyword", string)
                    .url(API.BaseApi_R + API.SearchLandmark_Api).build().execute(new StringCallback() {
                @Override
                public void onError(Request request, Exception e) {
                    T.showShort(LandMarkActivity.this, "网络连接失败，请检查一下网络设置");
                }

                @Override
                public void onResponse(String response) {
                    SearchLandMarkBean searchlandmarkbean = new Gson().fromJson(response, SearchLandMarkBean.class);
                    initHost(searchlandmarkbean);
                    initHouse(searchlandmarkbean);
                    initLandmark(searchlandmarkbean);


                }
            });


        }
    }

    //地标列表
    private void initLandmark(SearchLandMarkBean searchlandmarkbean) {
        final List<String> marklist = searchlandmarkbean.getData().getMarklist();
        if (marklist.size() == 0) {
            layoutLandmark.setVisibility(View.GONE);
        } else {

            lvLandmark.setAdapter(new LandMarkListAdapter(marklist, LandMarkActivity.this, "img_no"));
            layoutLandmark.setVisibility(View.VISIBLE);
        }
        lvLandmark.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                select = marklist.get(position);
                Intent intent = new Intent();
                intent.putExtra("select", select);
                setResult(2, intent);
                finish();
            }
        });

    }

    //房源标题列表
    private void initHouse(SearchLandMarkBean searchlandmarkbean) {
        final List<SearchLandMarkBean.DataBean.HouselistBean> houslistbean = searchlandmarkbean.getData().getHouselist();
        List<String> houselist = new ArrayList<String>();
        for (int i = 0; i < houslistbean.size(); i++) {
            houselist.add(houslistbean.get(i).getHTitle());
        }
        if (houselist.size() == 0) {
            layoutHouse.setVisibility(View.GONE);
        } else {

            lvHouse.setAdapter(new LandMarkListAdapter(houselist, LandMarkActivity.this, "img_no"));
            layoutHouse.setVisibility(View.VISIBLE);
        }
        lvHouse.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent2 = new Intent(LandMarkActivity.this, HouseDetailsActivity.class);
                intent2.putExtra("id", houslistbean.get(position).getId() + "");
//                intent2.putExtra("city",houslistbean.get(position).get)
                LandMarkActivity.this.startActivity(intent2);
            }
        });
    }

    //房东昵称列表
    private void initHost(SearchLandMarkBean searchlandmarkbean) {
        final List<SearchLandMarkBean.DataBean.HostlistBean> hostlistbean = searchlandmarkbean.getData().getHostlist();
        List<String> hostlist = new ArrayList<String>();
        for (int i = 0; i < hostlistbean.size(); i++) {
            hostlist.add(hostlistbean.get(i).getUNickname());
        }
        if (hostlist.size() == 0) {
            layoutHost.setVisibility(View.GONE);

        } else {

            lvHost.setAdapter(new LandMarkListAdapter(hostlist, LandMarkActivity.this, "img_no"));
            layoutHost.setVisibility(View.VISIBLE);
        }
        lvHost.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent2 = new Intent(LandMarkActivity.this, LandlordInfoActivity.class);
                intent2.putExtra("id", hostlistbean.get(position).getId() + "");
                LandMarkActivity.this.startActivity(intent2);
            }
        });
    }

    public void initData() {
        LoadingDialog.show(this);
        OkHttpUtils.get().addParams("token", SPUtils.get(this, "token", "") + "").addParams("cityname", city)
                .url(API.BaseApi_R + API.Landmark_Api).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                LoadingDialog.dismiss(LandMarkActivity.this);
                T.showShort(LandMarkActivity.this, "网络连接失败，请检查一下网络设置");
            }

            @Override
            public void onResponse(String response) {
                LoadingDialog.dismiss(LandMarkActivity.this);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    mVals2 = new String[jsonArray.length()];
                    for (int i = 0; i < jsonArray.length(); i++) {
                        mVals2[i] = (String) jsonArray.get(i);


                    }
                    LayoutInflater mInflater = LayoutInflater.from(LandMarkActivity.this);
                    for (int i = 0; i < mVals2.length; i++) {
                        final TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                                mFlowLayout, false);
                        tv.setText(mVals2[i]);
                        tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                select = tv.getText() + "";
                                Intent intent = new Intent();
                                intent.putExtra("select", select);
                                setResult(2, intent);
                                finish();
                            }
                        });
                        mFlowLayout.addView(tv);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    @OnClick({R.id.back, R.id.ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ok:
                Intent intent = new Intent();
                intent.putExtra("select", edtext.getText()+"");
                setResult(2, intent);
                finish();
                break;
        }
    }
}
